package com.allwees.support.payment.paypal.builder;

import com.paypal.orders.AddressPortable;
import com.paypal.orders.AmountBreakdown;
import com.paypal.orders.AmountWithBreakdown;
import com.paypal.orders.Item;
import com.paypal.orders.Money;
import com.paypal.orders.PurchaseUnitRequest;
import com.paypal.orders.ShippingDetail;
import com.allwees.bs.core.model.order.entity.OrderItemModel;
import com.allwees.bs.core.model.order.entity.OrderModel;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 组装paypal请求参数
 */
public class PurchaseUnitRequestBuilder {

    public static List<PurchaseUnitRequest> build(OrderModel order, List<? extends OrderItemModel> orderItems) {
        return new ArrayList<PurchaseUnitRequest>(1) {
            {
                add(buildOne(order, orderItems));
            }
        };
    }

    public static PurchaseUnitRequest buildOne(OrderModel order, List<? extends OrderItemModel> orderItems) {
        return buildOrder(order)
                .items(buildOrderItems(order.getCurrency(), order.getAmtSaleTax(), orderItems));
    }

    public static PurchaseUnitRequest buildOrder(OrderModel order) {

        AmountBreakdown amountBreakdown = new AmountBreakdown()
                .itemTotal(new Money().currencyCode(order.getCurrency()).value(order.getAmtProduct().toPlainString()))
                .shipping(new Money().currencyCode(order.getCurrency()).value(order.getAmtShipping().toPlainString()))
                .taxTotal(new Money().currencyCode(order.getCurrency()).value(order.getAmtSaleTax().toPlainString()));

        //组装优惠
        if (order.getAmtCash() != null) {
            amountBreakdown.discount(new Money().currencyCode(order.getCurrency()).value(order.getAmtCash().toPlainString()));
        }

        return new PurchaseUnitRequest()
                .referenceId("PUHF")
                .description(order.getNo()).customId(order.getUuid())
                .shippingDetail(shippingDetail(order))
                .amountWithBreakdown(new AmountWithBreakdown().currencyCode(order.getCurrency()).value(order.getAmt().toPlainString())
                        .amountBreakdown(
                                amountBreakdown
                        ));
    }


    public static List<Item> buildOrderItems(String currency, BigDecimal amtSaleTax, List<? extends
            OrderItemModel> orderItems) {
        List<Item> items = new ArrayList<>(orderItems.size());
        orderItems.forEach(o -> items.add(buildOrderItem(currency, amtSaleTax, o)));
        return items;
    }

    public static Item buildOrderItem(String currency, BigDecimal tax, OrderItemModel orderItem) {
        return new Item()
                .name(orderItem.getProductUuid())
                .sku(orderItem.getSkuUuid())
                .unitAmount(new Money().currencyCode(currency).value(orderItem.getAmtSku().toPlainString()))
                .quantity(String.valueOf(1));
    }



    private static ShippingDetail shippingDetail(OrderModel order) {
        return new ShippingDetail()
                .addressPortable(
                        new AddressPortable()
                                .adminArea1(order.getShippingAddressCountry())
                                .adminArea2(order.getShippingAddressProvince())
//                .adminArea3(order.getShippingAddressCity())
                                .addressLine1(order.getShippingAddressLine1())
                                .addressLine2(order.getShippingAddressLine2())
                                .countryCode("US")
                                .postalCode(order.getShippingAddressZipCode())
//            )
//            .name(
//                  new Name()
//                  .givenName(order.getShippingAddressFirstName())
//                  .surname(order.getShippingAddressLastName())
                );
    }
}
